package com.lightjet.macross.base.upms.service.impl;

import com.lightjet.macross.base.upms.domain.ResourceInfo;
import com.lightjet.macross.base.upms.domain.RoleInfo;
import com.lightjet.macross.base.upms.domain.RoleResourceLink;
import com.lightjet.macross.base.upms.repository.RoleResourceLinkRepository;
import com.lightjet.macross.base.upms.service.RoleResourceLinkService;
import com.lightjet.macross.common.core.BaseServiceImpl;
import com.lightjet.macross.common.dto.ResponseDTO;
import com.lightjet.macross.common.util.RedisUtil;
import com.lightjet.macross.common.util.RespUtil;
import lombok.extern.java.Log;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Log
public class RoleResourceLinkServiceImpl extends BaseServiceImpl<RoleResourceLink,Integer> implements RoleResourceLinkService {

    @Resource
    private RedisUtil redisUtil;

    @Override
    public List<RoleResourceLink> FindByRoleinfo(RoleInfo roleInfo) {
        return ((RoleResourceLinkRepository)baseRepository).findByRoleinfoOrderByResourceInfo_Orderno(roleInfo);
    }

    @Override
    @Transactional
    public ResponseDTO<String> UpdateResources(RoleInfo role, List<ResourceInfo> resources) {
        try {
            List<RoleResourceLink> owned = FindByRoleinfo(role);
            for (int i = owned.size() - 1; i >= 0; i--) {
                if(resources.size()==0)
                {
                    this.DeleteOne(owned.get(i));
                    owned.remove(i);
                }
                else
                {
                    for (int j = resources.size() - 1; j >= 0; j--) {
                        if (owned.get(i).getResourceInfo().getId().equals(resources.get(j).getId()))//已有项在更新集中存在，说明更新集中不需要再维护一遍直接删除跳出循环
                        {
                            resources.remove(j);
                            break;
                        }
                        if (j == 0)//已有项未在最新集中出现，需要删除
                        {
                            this.DeleteOne(owned.get(i));
                            owned.remove(i);
                        }
                    }
                }

            }
            for (ResourceInfo resource : resources) {
                RoleResourceLink newRelation = new RoleResourceLink(role,resource);

                this.CreateAndFlush(newRelation);
            }
            // 清空Redis //此处待调整
            ClearCache();
            return RespUtil.CreateOK();
        }
        catch (Exception e)
        {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return RespUtil.CreateError(MessageFormat.format("更新角色【{0}】的权限失败：{1}",role.getId(),e.getMessage()));
        }

    }

    @Override
    public void ClearCache() {
        // redisUtil.DeleteKeys("user::auth_userid:*");
        if(redisUtil!= null){
            redisUtil.DeleteKeys("user::resource_userid:*");

            log.info("删除所有auth_userid、menu_userid 缓存");
        }

    }
}
